 ///
 /// @file    iterator.cc
 /// @author  lemon(haohb13@gmail.com)
 /// @date    2016-03-17 14:47:34
 ///
 
#include "iterator.h"
#include <iostream>

using std::cout;
using std::endl;

ConcreateAggregate::ConcreateAggregate(int size)
: _size(size)
, _pData(new Data[_size])
{
	cout << "ConcreateAggregate(int)" << endl;
	for(int idx = 0; idx != _size; ++idx)
	{
		_pData[idx] = idx;
	}
}

Iterator * ConcreateAggregate::createIterator()
{
	return new ConcreateIterator(this);
}

ConcreateAggregate::~ConcreateAggregate()
{
	cout << "~ConcreateAggregate()" << endl;
	delete [] _pData;
}

int ConcreateAggregate::size()
{
	return _size;
}
Data ConcreateAggregate::getItem(int idx)
{
	return _pData[idx];
}


ConcreateIterator::ConcreateIterator(Aggregate * pAggregate)
: _idx(0)
, _pAggregate(pAggregate)
{
	cout << "ConcreateIterator(Aggregate*)" << endl;
}

void ConcreateIterator::first()
{
	_idx = 0;
}

void ConcreateIterator::next()
{
	if(_idx < _pAggregate->size())
	{
		++_idx;
	}
}

bool ConcreateIterator::isDone()
{
	return _idx == _pAggregate->size();
}

Data ConcreateIterator::currentItem()
{
	return _pAggregate->getItem(_idx);
}
